package com.interagriculture.controller;

import com.interagriculture.annotation.AdminVerify;
import com.interagriculture.common.CustomException;
import com.interagriculture.common.Result;
import com.interagriculture.entity.Competition;
import com.interagriculture.enums.ExceptionCodeEnum;
import com.interagriculture.service.CompetitionService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author 11 2024/11/12
 */
@RestController
@RequestMapping("/adminCompetition")
public class AdminCompetitionController {

    @Resource
    private CompetitionService competitionService;

    @PostMapping("/saveOrUpdateInfo")
    @AdminVerify
    public Result saveOrUpdateInfo(Integer id,
                                   String competitionName,
                                   String achievement,
                                   String participant,
                                   String attendTime,
                                   @RequestPart(required = false) MultipartFile picture){
        Competition competition = new Competition();
        competition.setCompetitionName(competitionName);
        competition.setId(id);
        competition.setAchievement(achievement);
        competition.setParticipant(participant);
        competition.setAttendTime(attendTime);
        competitionService.saveOrUpdateInfo(competition,picture);
        return Result.ok();

    }

    @GetMapping("/{id}")
    @AdminVerify
    public Result<Competition> getById(@PathVariable @NotNull Integer id){
        return Result.ok(competitionService.queryById(id));
    }

    @PostMapping("/delete")
    @AdminVerify
    public Result<Competition> deleteById(@RequestParam @NotNull Integer id){
        boolean deleteRes = competitionService.deleteById(id);
        if(!deleteRes){
            throw new CustomException(ExceptionCodeEnum.DELETE_ERROR);
        }
        return Result.ok();
    }
}
